Rules for Cleaning the Directory

Contents|Index|Previous|Next
Rules for cleaning the directory

Compiling a program is not the only thing for which you might want to write rules. Makefiles commonly tells how to do a few other things besides compiling a program; for instance, how to delete all the object files and executables so that the directory is clean. The following shows how to write a make rule for cleaning the example editor.

clean:
rm edit $(objects)

In practice, you might want to write the rule in a somewhat more complicated manner to handle unanticipated situations. Use input like the following example.

.PHONY : clean
clean :
-rm edit $(objects)

This prevents make from using an actual file called clean allowing it to continue in spite of errors from rm. (See Phony targets and in Errors in commands.) A rule such as this should not be placed at the beginning of the makefile, since you do not want it to run by default! Thus, in the example makefile, you want the rule for edit which recompiles the editor, to remain the default goal. Since clean is not a dependency of edit, this rule will not run at all if we give the command make with no arguments. In order to make the rule run, use make clean. See How to run make.

Top|Contents|Index|Previous|Next